package dao;

import pojo.News;
import util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 郝雷蒙
 * @Description
 * @date 2020/11/10 12:12
 */
public class NewsDaoImpl implements NewsDao {


    //修改view表中的viewcount 根据Id
    @Override
    public Integer updateViewCount(Integer count,Integer id) {
        String sql = "update view set viewCount=? where viewid=?";

        JDBCUtil jdbcUtil = new JDBCUtil();

        Integer status = jdbcUtil.excuteUpdate(sql,false,count,id);

        try {
            if (status>0) {
                return status;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();

            return null;
        } finally {
            jdbcUtil.close();
        }
    }



    @Override
    public Integer selectNewsCount(Map<String,Object> params) {
        String sql = "select count(*) from news ";
        sql +=createCondition(params);

        System.out.println(sql);

        JDBCUtil jdbcUtil = new JDBCUtil();

        ResultSet resultSet = jdbcUtil.excuteQuery(sql);

        try {
            if (resultSet.next()) {
                return resultSet.getInt(1);
            } else {
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();

            return null;
        } finally {
            jdbcUtil.close();
        }
    }

    @Override
    /**
     * 没写完
     */
    public List<News> selectAllNews(Map<String, Object> params) {
        List<News> newss = new ArrayList<>();
        String sql ="select n.*, a.authorName,v.viewCount from news n inner join author a on n.authorId = a.authorId inner join view v on v.viewId = n.viewId ";

        Integer start =(Integer) params.get("start") ;
        Integer length = (Integer) params.get("length");

        //拼接where 子句

        sql+=createCondition(params);

        sql+="order by newsDate desc limit ?, ?";

        System.out.println(sql);

        JDBCUtil jdbcUtil = new JDBCUtil();

        ResultSet resultSet = jdbcUtil.excuteQuery(sql,start,length);

        try {
            while (resultSet.next()){
                News news = new News();

                news.setNewsId(resultSet.getInt("newsId"));
                news.setNewsName(resultSet.getString("newsName"));
                news.setNewsDate(resultSet.getDate("newsDate"));
                news.setViewId(resultSet.getInt("viewId"));
                news.setViewCount(resultSet.getInt("viewCount"));
                news.setAuthorId(resultSet.getInt("authorId"));
                news.setAuthorName(resultSet.getString("authorName"));
                newss.add(news);
            }
            return newss;
        } catch (SQLException e) {
            e.printStackTrace();

            return null;
        } finally {
            jdbcUtil.close();
        }

    }


    //生成where条件子句
    private String createCondition(Map<String, Object> params) {
        StringBuffer buffer = new StringBuffer();

        String name = (String) params.get("name");
        if (name != null && !name.equals("")) {
            buffer.append(" and newsName like '%" + name + "%' ");
        }

        String startTime = (String) params.get("startTime");
        if (startTime != null &&!startTime.equals("")) {


            buffer.append("and newsDate > '" + startTime + "' ");
        }

        String afterTime = (String) params.get("afterTime");
        if (afterTime != null&& !afterTime.equals("") ) {

            buffer.append(" and newsDate < '" + afterTime + "' ");
        }


        if (buffer.length() > 0) {
            return "where 1 " + buffer.toString();
        }
        else {
            return "";
        }
    }


    @Override
    public News selectOneById(Integer id) {

       // String sql = "select * from news where newsid =?";
        String sql = "select n.*, a.authorName,v.viewCount from news n inner join author a on n.authorId = a.authorId inner join view v on v.viewId = n.viewId where n.viewId =?";

        JDBCUtil jdbcUtil = new JDBCUtil();
        List<News> newss = new ArrayList<>();
        ResultSet resultSet = jdbcUtil.excuteQuery(sql,id);

        try {

            while (resultSet.next()) {
                News news = new News();

                news.setNewsId(resultSet.getInt("newsId"));
                news.setNewsName(resultSet.getString("newsName"));
                news.setNewsDate(resultSet.getDate("newsDate"));
                news.setViewId(resultSet.getInt("viewId"));
                news.setViewCount(resultSet.getInt("viewCount"));
                news.setAuthorId(resultSet.getInt("authorId"));
                news.setAuthorName(resultSet.getString("authorName"));
                news.setNewsContent(resultSet.getString("newsContent"));

                newss.add(news);
            }

            return newss.size()>0?newss.get(0):null;
        } catch (SQLException e) {
            e.printStackTrace();

            return null;
        } finally {
            jdbcUtil.close();
        }

    }
}
